# To change this template, choose Tools | Templates
# and open the template in the editor.


from hexconfig import *



class SpikeLerper():
    def __init__(self,unit,endloc,spikeImgs,spikeRect):
        self.startloc = unit.drawLoc
        self.endloc = endloc
        self.rangeSize = [self.endloc[0]-self.startloc[0],self.endloc[1]-self.startloc[1]]
        self.rate = 0.000003
        self.n = 0.0
        self.unit = unit
        #self.unit.setImgIndex(2)#eyes unlit sprite
        self.spikeImgs = spikeImgs
        self.finished = False
        #self.shift = shift #this is the screen displacement of the spikes, so that
        self.spikeRect = spikeRect

    def update(self,etime):
        if self.finished:
            return   #this shouldnt really be a thing because it should be removed but no matter what it will just
                    #stop this way
        #print "hell"
        """
        if self.timec>self.endtime:
            self.timec = self.endtime
            self.finished = True
            if not self.unit.dead:
                self.unit.drawLerpMode = False
                self.unit.hexloc = self.endHex
            if self.pushing:
                self.unit.setImgIndex(0)"""
        #n = self.timec/self.endtime

        if random.random()<0.25:
            self.rate -= (0.000004)
            if self.rate < 0.000009:
                self.rate = 0.00009
        self.n += self.rate *etime
        self.unit.drawLoc = [self.startloc[0]+self.rangeSize[0]*self.n,\
                                self.startloc[1]+self.rangeSize[1]*self.n]

        if dist(self.unit.drawLoc,self.endloc)<16:
            self.finished

        if self.n >1.0:
            self.finished = True
            self.n = 1.0
            self.unit.drawLoc = [self.startloc[0]+self.rangeSize[0]*self.n,\
                                self.startloc[1]+self.rangeSize[1]*self.n]

    def draw(self,surf):

        imgRect = self.unit.colRect.copy()#img.get_rect(center = self.unit.drawLoc).copy()
        #bottom = imgRect.bottom
        #imgRect.height =2
        #imgRect.bottom = bottom
        imgRect.inflate_ip(-8,-8)
        subRect = imgRect.copy()
        #print subRect
        subRect = subRect.clip(self.spikeRect)
        #subRect.inflate_ip()
        #print subRect
        #subRect.top+=self.spikeRect[1]
        #subRect.left+=self.spikeRect[0]
        #screenLoc
        self.spikeImgs[0].blit(self.spikeImgs[1],(subRect.left-self.spikeRect.left,\
                                    subRect.top-self.spikeRect.top),(subRect.left-self.spikeRect.left,\
                                    subRect.top-self.spikeRect.top,subRect.width,subRect.height))
        #print subRect
        #surf.blit(self.spikeImgs[0],(0,0))#self.spikeRect.topleft)
        #print "BReah"
        return



class LocLerper():
    def __init__(self,unit,endloc,time,delay = 0.0,usingHexLoc = True,pushing = False):

        self.startloc = getPosFromHexLoc(unit.hexloc,True)
        if usingHexLoc:
            self.endloc = getPosFromHexLoc(endloc,True)
        else:
            self.endloc = endloc

        self.rangeSize = [self.endloc[0]-self.startloc[0],self.endloc[1]-self.startloc[1]]
        self.timec = 0.0
        self.endtime = time
        self.delay = delay
        self.delayActive = False if delay == 0.0 else True
        self.endHex = endloc if usingHexLoc else (200,200,200)
        self.finished = False
        self.unit = unit
        if not self.delayActive:
            self.unit.drawLerpMode = True

        n = self.timec/self.endtime
        self.unit.drawLoc = [self.startloc[0]+self.rangeSize[0]*n,self.startloc[1]+self.rangeSize[1]*n]
        self.pushing = pushing

    def update(self,etime):
        self.timec +=etime
        if self.delayActive:
            if self.timec>self.delay:
                self.delayActive = False
                self.timec = 0.0
                self.unit.drawLerpMode = True
            return

        if self.pushing and self.timec>self.endtime-400:
            self.unit.setImgIndex(1,1)

        if self.timec>self.endtime:
            self.timec = self.endtime
            self.finished = True
            if not self.unit.dead:
                self.unit.drawLerpMode = False
                self.unit.hexloc = self.endHex
            if self.pushing:
                self.unit.setImgIndex(0)
        n = self.timec/self.endtime
        self.unit.drawLoc = [self.startloc[0]+self.rangeSize[0]*n,self.startloc[1]+self.rangeSize[1]*n]

    def draw(self,surf):
        return


class BoomerangLerper(LocLerper):
    def __init__(self,unit,loc,time,delay = 0.0):#,firstHalf = True):
        time *=1.0
        LocLerper.__init__(self,unit,loc,time,delay)
        self.startLoc = unit.hexloc
        self.drawHold = (0,0)
        self.firstHalf = True
        self.holdSet = False
        self.startingDir = unit.facingLeft
        if self.rangeSize == 0.0:
            pass
        elif self.rangeSize[0]<0:
            self.unit.facingLeft = True
        else:
            self.unit.facingLeft = False
    def update(self,etime):
        LocLerper.update(self,etime)
        if not self.holdSet and self.firstHalf and self.timec> self.endtime*0.3:
                self.holdSet = True
                self.drawHold = self.unit.drawLoc

        if self.holdSet and self.firstHalf and self.timec>self.endtime*0.3:
            self.unit.drawLoc = self.drawHold

        if self.holdSet and not self.firstHalf and self.timec<self.endtime*0.7:
            self.unit.drawLoc = self.drawHold

        if self.firstHalf and self.timec > self.endtime - 280:
            self.unit.setImgIndex(1, 1)
        if self.firstHalf and self.timec > self.endtime - 100:
            self.unit.setImgIndex(1, 0)
        if not self.firstHalf and self.unit.imgState == 1:
            self.unit.setImgIndex(0)
        if self.finished:#and not self.firstHalf:#self.timec>self.endtime:
            if self.firstHalf:
                LocLerper.__init__(self,self.unit,self.startLoc,self.endtime,20.0)
                self.unit.drawLerpMode = True
                self.unit.drawLoc = self.drawHold

                #print self.finished
            else:
                self.unit.facingLeft = self.startingDir
                return

            self.firstHalf = False
            self.unit.facingLeft = not self.unit.facingLeft
            return



class JumpLerper():
    def __init__(self,unit,endloc,time,delay = 0.0,usingHexLoc = True,isDying = False,dazing = False):
        self.startloc = getPosFromHexLoc(unit.hexloc,True)
        if usingHexLoc:
            self.endloc = getPosFromHexLoc(endloc,True)
        else:
            self.endloc = endloc
        self.rangeSize = [self.endloc[0]-self.startloc[0],self.endloc[1]-self.startloc[1]]
        self.timec = 0
        self.endtime = time
        if usingHexLoc:
            self.endHex = endloc
        else:
            self.endHex = (200,200,200)


        self.delay = delay
        self.delayActive = False if delay == 0.0 else True
        self.timec = 0.0

        self.finished = False
        self.unit = unit
        self.unit.drawLerpMode = True

        self.jumpHeight = 0.0
        self.unit.jumping = True
        self.unit.velUp = True
        #self.jumpVel = -0.21
        self.jumpVel = -0.5*GRAVITY*time
        self.rotSpd = random.random()*0.8-0.4 if isDying else 0.0

        self.locLerp = LocLerper(unit,endloc,time,0.0,usingHexLoc)

        #if not self.delayActive:
        self.unit.drawLerpMode = True

        self.isDying = isDying
        self.dazing = dazing

    def update(self,etime):
        self.timec+=etime
        if self.delayActive:
            if self.timec>self.delay:
                if self.dazing:
                    self.unit.dazed =  True
                self.delayActive = False
                self.timec = 0.0
                self.unit.drawLerpMode = True
            return

        self.locLerp.update(etime)
        self.jumpVel += GRAVITY * etime
        self.jumpHeight+=self.jumpVel * etime
        self.unit.drawLoc[1]+=self.jumpHeight

        if self.isDying:
            self.unit.rot+=self.rotSpd*etime
        if math.fabs(self.jumpVel)<0.035:
            self.unit.setImgIndex(3,1)


        elif self.jumpVel<0:
            self.unit.setImgIndex(3,0)
        else:
            self.unit.setImgIndex(3,2)
            #print "HElo"


        if self.locLerp.finished or self.jumpHeight>0:
            if not self.unit.dead:
                self.unit.hexloc = self.endHex
                self.unit.setImgIndex(2,0)
                self.unit.drawLerpMode = False
            else:
                self.unit.hexloc = (200,200,200)
                self.unit.drawLoc = self.endloc

            self.finished = True
            self.unit.jumping = False



    def draw(self,surf):
        self.unit.draw(surf)

